#### clear the working environment

#### load the required packages ####
library(tidyverse)
library(PanelMatch)
library(countrycode)
library(patchwork)

#### load the replication data ####
df <- readRDS("data/dem-transitions-replication-data.rds") %>%
  glimpse()


#### Note: These analyses are referenced in the text of the section entitled "Changing Institutions and Preferences." #### 

#### argentina percentile of free expression and civil society in 1983 (pre-transition) ####
df_1983 <- df %>%
  filter(year == 1983) %>%
  glimpse()

# empirical cumulative distribution function of free expression for the full sample in 1983
d1 <- ecdf(df_1983$v2x_freexp_altinf)

# argentina free expression in 1983
v1 <- df_1983$v2x_freexp_altinf[df_1983$country==160]

# argentina percentile of free expression in 1983
p1 <- d1(v1)

# empirical cumulative distribution function of civil society for the full sample in 1983
d2 <- ecdf(df_1983$v2x_cspart)

# argentina free expression for argentina in 1983
v2 <- df_1983$v2x_cspart[df_1983$country==160]

# argentina percentile of civil society in 1983
p2 <- d2(v2)

#### argentina percentile of free expression and civil society in 1984 (post-transition) ####
df_1984 <- df %>%
  filter(year == 1984) %>%
  glimpse()

# empirical cumulative distribution function of free expression for the full sample in 1984
d3 <- ecdf(df_1984$v2x_freexp_altinf)

# argentina free expression in 1984
v3 <- df_1984$v2x_freexp_altinf[df_1984$country==160]

# argentina percentile of free expression in 1984
p3 <- d1(v3)

# empirical cumulative distribution function of civil society for full sample in 1984
d4 <- ecdf(df_1984$v2x_cspart)

# argentina civil society in 1984
v4 <- df_1984$v2x_cspart[df_1984$country==160]

# argentina percentile of civil society in 1984
p4 <- d4(v4)

